document.addEventListener('deviceready', function () {

	$("#take-photo").click( takePhoto );
	$("#choose-photo").click( choosePhoto );

}, false);

function takePhoto() {

	var cameraOptions = {
		quality: 75,
		destinationType: Camera.DestinationType.DATA_URL,
		sourceType: Camera.PictureSourceType.CAMERA,
		saveToPhotoAlbum: true,

		//fix orientation issues
		correctOrientation: true,
		encodingType: Camera.EncodingType.JPEG,
		targetWidth: 720
	};
	navigator.camera.getPicture( onTakeSuccess, onTakeError, cameraOptions );
}



function onTakeSuccess( imageData ) {

	var image=document.createElement("img");
	image.src = "data:image/jpeg;base64," + imageData;

	imgList.push(image.src);
	albs[1]=imgList;
	user.albList[albumIdx]=albs;
	localStorage.setItem(user.name,JSON.stringify(user));

	image.setAttribute("data-positionId",imgList.indexOf(image.src));
	document.getElementById("photoWall").appendChild(image);
	if (image.width < 135) {
		image.setAttribute("class","img_style_2");
		//$($("#photoWall img")[i]).addClass("img_style_2");
	}else{
		image.setAttribute("class","img_style_1");
	}
	photo.init();

}


function choosePhoto() {

	var cameraOptions = {
		quality: 75,
		destinationType: Camera.DestinationType.FILE_URI,
		sourceType: Camera.PictureSourceType.PHOTOLIBRARY,

		//fix orientation issues
		correctOrientation: true,
		encodingType: Camera.EncodingType.JPEG,
		targetWidth: 720
	};

	navigator.camera.getPicture( onGetSuccess, onTakeError, cameraOptions );

}

function onGetSuccess( imageURI ) {

	var image=document.createElement("img");
	image.src = "data:image/jpeg;base64," + imageData;

	imgList.push(image.src);
	albs[1]=imgList;
	user.albList[albumIdx]=albs;
	localStorage.setItem(user.name,JSON.stringify(user));

	image.setAttribute("data-positionId",imgList.indexOf(image.src));
	document.getElementById("photoWall").appendChild(image);
	if (image.width < 135) {
		image.setAttribute("class","img_style_2");
		//$($("#photoWall img")[i]).addClass("img_style_2");
	}else{
		image.setAttribute("class","img_style_1");
	}
	photo.init();

}


function onTakeError( message ) {
	alert( "Failed: " + message );
}



